package com.Buttonbar;

import com.Welcome.R;
 

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class AndroidFifth extends ActivityGroup implements View.OnClickListener {
	private final static Class<?>[] sActivityClasses = { Activity1.class,
			Activity2.class, Activity3.class, Activity4.class, Activity5.class };

	private final static int[] sResIds = { R.id.btn1, R.id.btn2, R.id.btn3,
			R.id.btn4, R.id.btn5 };

	private final static String[] sActivityIds = { "Activity1", "Activity2",
			"Activity3", "Activity4", "Activity5" };

	private RelativeLayout mViewContainer;

	private Button[] mBtns = new Button[sResIds.length];

	private int mCurId = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activityfifth);
		setupViews();
	}

	private void setupViews() {
		mViewContainer = (RelativeLayout) findViewById(R.id.container);
		final Button[] btns = mBtns;
		for (int i = 0; i < btns.length; i++) {
			btns[i] = (Button) findViewById(sResIds[i]);
			btns[i].setText("Button" + i + 1);
			btns[i].setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		final int id = v.getId();
		if (mCurId == id) {
			return;
		}
		mCurId = id;
		processViews(id);
	}

	private void processViews(int rid) {
		mViewContainer.removeAllViews();
		final int index = getButtonIndex(rid);
		final View tempView = getLocalActivityManager().startActivity(
				sActivityIds[index],
				new Intent(this, sActivityClasses[index])
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
				.getDecorView();
		mViewContainer.addView(tempView);
	}

	private int getButtonIndex(int rid) {
		final int length = sResIds.length;
		for (int i = 0; i < length; i++) {
			if (rid == sResIds[i]) {
				return i;
			}
		}
		return 0;
	}
}
